﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Core;
using MvcEngine.Core.Extensions;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core.Helpers;
using MvcEngine.Mvc.ModelBinders;
using MvcEngine.Core.Common.Slugs;

namespace MvcEngine.Blog.ModelBinders
{
    public class BlogTagModelBinder : ModelBinderBase
    {
        public override object Bind(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.HttpContext.Request;
            BlogTagBase tag = IoC.Resolve<BlogTagBase>();
            tag.Name = request.Form.Get("Name").HtmlEncode();
            tag.Slug = request.Form.Get("Slug").HtmlEncode();
            Validate(tag, controllerContext);
            if (string.IsNullOrEmpty(tag.Slug) && !string.IsNullOrEmpty(tag.Name))
            {
                tag.Slug = IoC.Resolve<ISlugConverter>().Convert(tag.Name);
            }
            return tag;
        }
    }
}
